<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="cxFn">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="tjFn">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status=== 1 ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <!-- #default="scope" -->
          <template #default="scope">
            <el-button size="mini" type="text" @click="bianjFn(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delFn(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="pageSize"
        @current-change="pageChange"
      />
    </div>
    <!-- 添加员工 -->
    <el-dialog
      :title="titel==='添加员工' ? '添加员工' : '编辑员工' "
      width="480px"
      :visible.sync="dialogVisible"
      @close="closeFn"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="80px">
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="登录账号" prop="userName">
            <el-input v-model="addForm.userName" />
          </el-form-item>
          <el-form-item label="联系方式" prop="phonenumber">
            <el-input v-model="addForm.phonenumber" />
          </el-form-item>
          <el-form-item label="分配角色" prop="roleId">
            <el-select v-model="addForm.roleId" placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="员工状态" prop="status">
            <el-radio-group v-model="addForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeFn">取 消</el-button>
        <el-button size="mini" type="primary" @click="quedFn">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeListAPI, createEmployeeAPI, huiEmployeeAPI, delEmployeeAPI, editEmployeeAPI } from '@/api/system-employee.js'
import { getRoleListAPI } from '@/api/role.js'
export default {
  name: 'Employee',
  data() {
    return {
      titel: '添加员工',
      name: '',
      page: 1,
      pageSize: 5,
      employeeList: [],
      total: 0,
      dialogVisible: false,
      roleList: [],
      addForm: {
        name: '',
        userName: '',
        phonenumber: '',
        roleId: null,
        status: 1
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入登录账号', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请分配角色', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择员工状态', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeList()
    this.getRoleList()
  },
  methods: {
    async getEmployeeList() {
      const res = await getEmployeeListAPI({
        page: this.page,
        pageSize: this.pageSize,
        name: this.name
      })
      // console.log(res)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    cxFn() {
      this.page = 1
      this.getEmployeeList()
    },
    pageChange(page) {
      this.page = page
      this.getEmployeeList()
    },
    async getRoleList() {
      const res = await getRoleListAPI()
      // console.log(res)
      this.roleList = res.data
    },
    delFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployeeAPI(id)
        this.getEmployeeList()
        this.$message.success('删除成功')
      })
    },
    async quedFn() {
      if (this.titel === '添加员工') {
        this.$refs.addForm.validate(async isok => {
          if (isok) {
            await createEmployeeAPI(this.addForm)
            this.dialogVisible = false
            this.$message.success('添加成功')
            this.getEmployeeList()
            this.closeFn()
          }
        })
      } else {
        const data = { ...this.addForm }
        delete data.createTime
        await editEmployeeAPI(data)
        this.$message.success('编辑成功')
        this.dialogVisible = false
        this.closeFn()
        this.getEmployeeList()
      }
    },
    async bianjFn(id) {
      this.titel = '编辑员工'
      this.dialogVisible = true
      const res = await huiEmployeeAPI(id)
      this.addForm = res.data
    },
    tjFn() {
      this.dialogVisible = true
      this.titel = '添加员工'
    },
    closeFn() {
      this.dialogVisible = false
      this.addForm = {
        name: '',
        userName: '',
        phonenumber: '',
        roleId: null,
        status: 1
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>

